//
//  ViewController.m
//  SomeListDemo
//
//  Created by 孙哈哈 on 2017/10/20.
//  Copyright © 2017年 SunDe. All rights reserved.
//

#import "ViewController.h"
#import "MainTableView.h"
#import "SubTableVC.h"

#define SCREENW [UIScreen mainScreen].bounds.size.width
#define SCREENH [UIScreen mainScreen].bounds.size.height

@interface ViewController () <UITableViewDelegate, UITableViewDataSource, UICollectionViewDelegate, UICollectionViewDataSource, UIScrollViewDelegate>

@property (nonatomic, strong) MainTableView *tableView;
@property (nonatomic, strong) UICollectionView *mainCollection;
@property (nonatomic, assign) BOOL canScroll;

@property (nonatomic, strong) SubTableVC *subVC1;
@property (nonatomic, strong) SubTableVC *subVC2;
@property (nonatomic, strong) SubTableVC *subVC3;

@end

@implementation ViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view, typically from a nib.
    
    self.title = @"列表";
    
    self.tableView = [[MainTableView alloc] initWithFrame:CGRectMake(0, 64, SCREENW, SCREENH-64-49) style:UITableViewStylePlain];
    self.tableView.showsVerticalScrollIndicator = NO;
    [self.view addSubview:self.tableView];
    self.tableView.delegate = self;
    self.tableView.dataSource = self;
    self.tableView.rowHeight = SCREENH-64-49-40;
    self.canScroll = YES;

    self.subVC1 = [[SubTableVC alloc] init];
    __weak typeof(self) weakSelf = self;
    [self.subVC1 handlerBlock:^{
        
        NSLog(@"666_SunDePrint_999:%@", @"sub=Y");

        weakSelf.canScroll = YES;
        weakSelf.subVC2.canScroll = NO;
        weakSelf.subVC2.tableView.contentOffset = CGPointZero;
        weakSelf.subVC3.canScroll = NO;
        weakSelf.subVC3.tableView.contentOffset = CGPointZero;
    }];
    [self addChildViewController:self.subVC1];
    
    
    self.subVC2 = [[SubTableVC alloc] init];
    [self.subVC2 handlerBlock:^{
        
        weakSelf.canScroll = YES;
        weakSelf.subVC1.canScroll = NO;
        weakSelf.subVC1.tableView.contentOffset = CGPointZero;
        weakSelf.subVC3.canScroll = NO;
        weakSelf.subVC3.tableView.contentOffset = CGPointZero;
    }];
    [self addChildViewController:self.subVC2];
    
    self.subVC3 = [[SubTableVC alloc] init];
    [self.subVC3 handlerBlock:^{
        
        weakSelf.canScroll = YES;
        
        weakSelf.subVC1.canScroll = NO;
        weakSelf.subVC1.tableView.contentOffset = CGPointZero;
        weakSelf.subVC2.canScroll = NO;
        weakSelf.subVC2.tableView.contentOffset = CGPointZero;
    }];
    [self addChildViewController:self.subVC3];

    
    UIView *head = [[UIView alloc] initWithFrame:CGRectMake(0, 0, SCREENW, SCREENW/2)];
    head.backgroundColor = [UIColor orangeColor];
    self.tableView.tableHeaderView = head;
    [self setMainCrollView];
}

- (void)scrollViewDidScroll:(UIScrollView *)scrollView {
    
    if (scrollView == self.tableView) {
     
        CGFloat scrollY = [self.tableView rectForSection:0].origin.y;
        
        if (scrollView.contentOffset.y >= scrollY) {
            
            if (self.canScroll == YES) {
                
                self.canScroll = NO;
                
                self.subVC1.canScroll = YES;
                self.subVC1.tableView.contentOffset = CGPointZero;
                self.subVC2.canScroll = YES;
                self.subVC2.tableView.contentOffset = CGPointZero;
                self.subVC3.canScroll = YES;
                self.subVC3.tableView.contentOffset = CGPointZero;
            }
            
            self.tableView.contentOffset = CGPointMake(0, scrollY);
            
        }else{
            
            if (self.canScroll == NO) {
                self.tableView.contentOffset = CGPointMake(0, scrollY);
            }
        }
    }
}


#pragma mark 左右滑动的collectionview
- (void)setMainCrollView {
    
    UICollectionViewFlowLayout *layout = [[UICollectionViewFlowLayout alloc] init];
    layout.itemSize = CGSizeMake(SCREENW, SCREENH-64-49-40);
    layout.minimumLineSpacing = 0;
    layout.minimumInteritemSpacing = 0;
    layout.sectionInset = UIEdgeInsetsZero;
    layout.scrollDirection = UICollectionViewScrollDirectionHorizontal;
    
    self.mainCollection = [[UICollectionView alloc] initWithFrame:CGRectMake(0, 0, SCREENW, SCREENH-64-49-40) collectionViewLayout:layout];
    self.mainCollection.delegate = self;
    self.mainCollection.dataSource = self;
    self.mainCollection.showsHorizontalScrollIndicator = NO;
    self.mainCollection.pagingEnabled = YES;
    
    [self.mainCollection registerClass:[UICollectionViewCell class] forCellWithReuseIdentifier:@"UICollectionViewCellID"];
}

#pragma mark collectionview  delegate
- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section {
    return 3;
}

-(UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath{
    
    UICollectionViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"UICollectionViewCellID" forIndexPath:indexPath];
    cell.backgroundColor = [UIColor yellowColor];
    
    SubTableVC *subVC = self.childViewControllers[indexPath.row];
    subVC.view.frame = CGRectMake(0, 0, SCREENW, SCREENH-64-49-40);
    subVC.tableView.frame = CGRectMake(0, 0, SCREENW, SCREENH-64-49-40);
    [cell.contentView addSubview:subVC.view];
    return cell;
}

#pragma mark tableView delegate
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    
    return 1;
}

- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section {
    return 40;
}

- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section {
    
    UIView *sectionHead = [[UIView alloc] initWithFrame:CGRectMake(0, 0, SCREENW, 40)];
    sectionHead.backgroundColor = [UIColor redColor];
    for (int i = 0; i < 3; i++) {
        UIButton *btn = [[UIButton alloc] initWithFrame:CGRectMake(i*SCREENW/3, 0, SCREENW/3, 40)];
        [btn setTitle:@"按钮" forState:0];
        [sectionHead addSubview:btn];
    }
    return sectionHead;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"CELLID"];
    if (cell == nil) {
        cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:@"CELLID"];
        cell.selectionStyle = UITableViewCellSelectionStyleNone;
        [cell.contentView addSubview:self.mainCollection];
    }
    
    cell.contentView.backgroundColor = [UIColor greenColor];
    
    return cell;
}

@end
